package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.mapper.SystemMenuMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.SystemMenuService;
import cn.wolfcode.wms.vo.SystemMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SystemMenuServiceServiceImpl implements SystemMenuService {

    @Autowired
    private SystemMenuMapper systemMenuMapper;

    public List<SystemMenu> listAll() {
        return systemMenuMapper.selectAll();
    }

    public SystemMenu getById(Long id) {
        return systemMenuMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(SystemMenu systemMenu) {
        if (systemMenu.getId() != null) {
            systemMenuMapper.updateByPrimaryKey(systemMenu);
        } else {
            systemMenuMapper.insert(systemMenu);
        }
    }

    public void delete(Long id) {
        // 删除子菜单
        systemMenuMapper.deleteByParentId(id);
        // 删除自己
        systemMenuMapper.deleteByPrimaryKey(id);
    }

    public PageResult query(QueryObject qo) {
        int rows = systemMenuMapper.queryForCount(qo);
        if (rows == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<?> result = systemMenuMapper.queryForList(qo);
        return new PageResult(rows, result, qo.getCurrentPage(), qo.getPageSize());
    }

    public List<SystemMenu> getParents(Long parentId) {
        List<SystemMenu> ret = new ArrayList<>();
        // 1. 判断parentId不为null，如果为null就表示已经获取完父节点
        while (parentId != null) {
            // 1.1 根据当前的parentId查询出对应的菜单
            SystemMenu parent = systemMenuMapper.selectByPrimaryKey1(parentId);
            // 1.2 判断该父菜单不为空，如果不为空将其加入到结果列表中
            if (parent != null) {
                ret.add(parent);
                // 1.3 将该parentId覆盖原来的parentId
                parent = parent.getParent();
                if (parent != null) {
                    parentId = parent.getId();
                }
            }
            // 没有父菜单
            if (parent == null) {
                break;
            }
        }
        // 2. 将获取完成的结果进行反转
        Collections.reverse(ret);
        return ret;
    }

    public List<SystemMenuVO> loadMenu(String ParentSn) {
        List<SystemMenuVO> list = (List<SystemMenuVO>) systemMenuMapper.loadmenu(ParentSn);
        return list;


    }

    /**
     * 第二种方案
     */
    public List<SystemMenu> getParents1(Long parentId) {
        List<SystemMenu> ret = new ArrayList<>();
        // 1. 判断parentId不为null，如果为null就表示已经获取完父节点
        SystemMenu parent = systemMenuMapper.selectByPrimaryKey1(parentId);
        while (parent != null) {
            ret.add(parent);
            parent = parent.getParent();
        }
        // 2. 将获取完成的结果进行反转
        Collections.reverse(ret);
        return ret;
    }
}
